import 'package:flutter/material.dart';
import 'package:citiesocial/_include.dart';
import 'package:citiesocial/ui/user/not_login.dart';

///首页-收藏
class Collection extends BaseStatefulWidget {
  @override
  State<StatefulWidget> createState() => _CollectionState();
}

class _CollectionState extends BaseState<Collection> {
  var _page = 1;
  var _loadable = true;
  final goodsList = List<GoodsEntity>();
  var _offstage = true;

  void _loadData() async {
    LoadingDialog.show(context);
    final list = await HttpManager.focusList(Configs.userId, Configs.userToken,
        page: _page, context: context);
    LoadingDialog.dismiss(context);
    if (_page == 1) {
      goodsList.clear();
    }
    if (list != null && list.length > 0) {
      _loadable = list.length >= HttpManager.default_size;
      goodsList.addAll(list);
    } else {
      _loadable = false;
    }
    setState(() {});
  }

  void _deleteCollection() {
    String ids = '';
    if (goodsList.length > 0) {
      goodsList.forEach((goods) {
        if (goods.selectedMyState) {
          if (ids.length > 0) ids += ',';
          ids += goods.goods_id;
        }
      });
    }
    if (ids.length == 0) {
      Toast.show(context, '未选中任何商品');
    } else {
      HttpManager.deleteMoreFocus(ids, Configs.userId, Configs.userToken,
              context: context)
          .then((yes) {
        if (yes) _loadData();
      });
    }
  }

  @override
  void initState() {
    if (Configs.isLogined) {
      Future.delayed(Configs.loadDelay, () {
        _loadData();
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarUtils.collect(context, _offstage ? '编辑' : '取消', () {
        setState(() {
          _offstage = !_offstage;
        });
      }),
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Expanded(
              child: !Configs.isLogined
                  ? Align(
                      child: NotLogin('登录会员，查看收藏内容'),
                      alignment: Alignment.topCenter,
                    )
                  : DListView(
                      onRefresh: () async {
                        _loadable = true;
                        _page = 1;
                        _loadData();
                      },
                      onLoadmore: () {
                        if (_loadable) {
                          _page++;
                          _loadData();
                        }
                      },
                      itemCount: goodsList.length,
                      itemBuilder: (_, index) => LongSquareGoodsItem(
                            goodsList[index],
                            offstage: _offstage,
                            onTap: _offstage
                                ? null
                                : () {
                                    setState(() {
                                      goodsList[index].selectedMyState =
                                          !goodsList[index].selectedMyState;
                                    });
                                  },
                          ),
                    ),
            ),
            Offstage(
              offstage: _offstage,
              child: GestureDetector(
                  child: Container(
                    width: double.infinity,
                    padding: EdgeInsets.all(15.0),
                    color: Colors.red,
                    child: Center(
                      child: Text(
                        '删除收藏',
                        style: TextStyle(fontSize: 18.0, color: Colors.white),
                      ),
                    ),
                  ),
                  onTap: _deleteCollection),
            ),
          ],
        ),
      ),
    );
  }
}
